﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows.Input;
using System.Xml;
using System.Xml.Serialization;


namespace SQLDeployAnything.Data.Files
{
    [Serializable()]
    [XmlRoot(ElementName ="Environments")]
    public class Connections : INotifyPropertyChanged
    {
        private ObservableCollection<Environment.Environment> mEnvironments;
        private string mFile;

        public Connections()
        {
            createCommands();
            mEnvironments = new ObservableCollection<Environment.Environment>();
        }

        [XmlIgnore]
        public string ConnectionFile
        {
            get { return mFile; }
            set
            {
                string filepath = value;
                if (filepath != mFile)
                {
                    //TODO perform validations before setting and loading file.

                    mFile = filepath;
                    OnPropertyChanged("ConnectionsFile");
                }
            }
        }

        public ObservableCollection<Environment.Environment> Environments
        {
            get{ return mEnvironments;}
            set
            {
                mEnvironments = value;
                OnPropertyChanged("Environments");
            }
        }

        public void Save()
        {
            Serialize();
        }

        private void Serialize()
        {
            if (String.IsNullOrEmpty(ConnectionFile))
                throw new Exception("ConnectionFile not Specified.");

            StreamWriter sw = new StreamWriter(ConnectionFile, false);
            
            XmlSerializer xml = new XmlSerializer(typeof(Connections));
            xml.Serialize(sw, this);
            sw.Close();
        }

        private static Connections Deserialize(string fileName)
        {
            Connections myObject;
            if (string.IsNullOrEmpty(fileName))
                throw new ArgumentNullException("fileName");
            if (!File.Exists(fileName))
                throw new FileNotFoundException("File Not Found", fileName);

            FileStream inStream = new FileStream(fileName, FileMode.Open);
            XmlSerializer xml = new XmlSerializer(typeof(Connections));

            myObject = (Connections)xml.Deserialize(inStream);
            myObject.ConnectionFile = fileName;

            return myObject;

        }

        public static Connections Open(string fileName)
        {
            return Deserialize(fileName);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected internal void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }


        private void createCommands()
        {
            AddEnvironmentCommand = new Commands.CreateEnvironment(this);
        }

        [XmlIgnore]
        public ICommand AddEnvironmentCommand
        {
            get;
            internal set;
        }

        internal Environment.Environment CreateEnvironment(string name)
        {
            Environment.Environment env = new Environment.Environment();
            env.Name = name;
            this.Environments.Add(env);
            return env;
        }
    
        
    }
}
